package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.SetMealDao;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckGroupService;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;

    //通过当前预约的id查询套餐信息(检查组,检查项信息)
    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setMealDao.findById(id);
        //创建一个集合,放检查组对象
        ArrayList<CheckGroup> checkGroupList = new ArrayList<>();
        //通过当前套餐的id查询出对应的检查组id
        List<Integer> checkGroupIds = setMealDao.findCheckGroupIdsBySetmealId(id);
        for (Integer checkGroupId : checkGroupIds) {
            CheckGroup checkGroup = checkGroupDao.findById(checkGroupId);
            checkGroupList.add(checkGroup);
        }
        //创建一个集合,放检查项对象
       // ArrayList<CheckItem> checkItemList = new ArrayList<>();
        for (CheckGroup checkGroup : checkGroupList) {
            ArrayList<CheckItem> checkItemList = new ArrayList<>();
            //查询检查组中对应的所有检查项的id
            List<Integer> checkItemIds = checkGroupDao.findCheckItemIdsByCheckGroupId(checkGroup.getId());
            for (Integer checkItemId : checkItemIds) {
                //通过每个检查组的id查到对应的检查项id
                CheckItem checkItem = checkItemDao.findById(checkItemId);
                checkItemList.add(checkItem);
            }
            checkGroup.setCheckItems(checkItemList);
           setmeal.setCheckGroups(checkGroupList);
        }
       // setmeal.setCheckGroups(checkGroupList);
        return setmeal;
    }
}
